﻿using System;
using System.Data;
using Think9.Models;
using Think9.Services.Base;

namespace Think9.Services.Basic
{
	public class TbIndex : BaseService<TbIndexEntity>
	{
		public static string GetDefaultValue(CurrentUserEntity user, DataTable dtIndex, string indexid)
		{
			string defaultV = "";
			string ruid = "";
			string ruleType = "";
			string _value = "";
			foreach (DataRow dr in dtIndex.Rows)
			{
				if (dr["IndexId"].ToString() == indexid)
				{
					ruid = dr["RuleId"].ToString();
					ruleType = dr["RuleType"].ToString();
					defaultV = dr["DefaultV"].ToString();
					break;
				}
			}

			if (defaultV != "")
			{
				_value = "";
				if (defaultV == "@sysusername" || defaultV == "＠sysusername")
				{
					_value = user == null ? "" : user.RealName; ;
				}

				if (defaultV == "@sysuserid" || defaultV == "＠sysuserid")
				{
					_value = user == null ? "" : user.Account;
				}

				if (defaultV == "@sysdeptno" || defaultV == "＠sysdeptno")
				{
					_value = user == null ? "" : user.DeptNo;
				}

				if (defaultV == "@sysdeptname" || defaultV == "＠sysdeptname")
				{
					_value = user == null ? "" : user.DeptName;
				}

				if (defaultV == "@sysroleno" || defaultV == "＠sysroleno")
				{
					_value = user == null ? "" : user.RoleNo;
				}

				if (defaultV == "@sysrolename" || defaultV == "＠sysrolename")
				{
					_value = user == null ? "" : user.RoleName;
				}

				if (defaultV == "@sysday" || defaultV == "＠sysday")
				{
					_value = DateTime.Today.ToString("yyyy-MM-dd");
				}

				if (defaultV == "@systime" || defaultV == "＠systime")
				{
					_value = DateTime.Now.ToString();
				}
				if (defaultV == "@systimenow" || defaultV == "＠systimenow")
				{
					_value = DateTime.Now.ToString();
				}

				if (_value == "")
				{
					_value = defaultV;
				}
			}
			else
			{
				if (ruleType == "1")
				{
					if (ruid == "sysusername")
					{
						_value = user == null ? "" : user.RealName; ;
					}

					if (ruid == "sysuserid")
					{
						_value = user == null ? "" : user.Account;
					}

					if (ruid == "sysdeptno")
					{
						_value = user == null ? "" : user.DeptNo;
					}

					if (ruid == "sysdeptname")
					{
						_value = user == null ? "" : user.DeptName;
					}

					if (ruid == "sysroleno")
					{
						_value = user == null ? "" : user.RoleNo;
					}

					if (ruid == "sysrolename")
					{
						_value = user == null ? "" : user.RoleName;
					}

					if (ruid == "sysday")
					{
						_value = DateTime.Today.ToString("yyyy-MM-dd");
					}

					if (ruid == "systime")
					{
						_value = DateTime.Now.ToString();
					}
					if (ruid == "systimenow")
					{
						_value = DateTime.Now.ToString();
					}
				}
			}
			return _value;
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="dsindex"></param>
		/// <param name="indexid">指标编码如v1</param>
		/// <param name="rownumber">第几行？</param>
		/// <param name="from">add或edit用于控制编辑时锁定</param>
		/// <returns></returns>
		public static string GetGridLockStr(DataTable dsindex, string indexid, int rownumber, string from)
		{
			string _lock = "";
			string isLock = "";
			string numLocked = "";
			foreach (DataRow dr in dsindex.Rows)
			{
				if (dr["IndexId"].ToString() == indexid)
				{
					isLock = dr["isLock"].ToString();//是否锁定？1是2否3按行锁定
					numLocked = " " + dr["isLock2"].ToString() + " ";//锁定的行号 以空格间隔
					break;
				}
			}

			//锁定
			if (isLock == "1")
			{
				_lock = "disabled='disabled'";
			}
			//按行锁定
			if (isLock == "3" && numLocked.Contains(" " + rownumber.ToString() + " "))
			{
				_lock = "disabled='disabled'";
			}
			//编辑时锁定
			if (isLock == "9" && from != "add")
			{
				_lock = "disabled='disabled'";
			}

			return _lock;
		}
	}
}